#include "enemy.h"
#include "Scene.h"
#include "game.h"
#include "gameinfo.h"
#include "turret.h"
//#include <bits/stdc++.h>
#include <QPoint>
#include <QPainter>
#include <QColor>
#include <QDebug>
#include <QMovie>
Enemy::Enemy()
{

}

Enemy::Enemy(QPoint pos, int type, Game *game)
{
    this->pos=pos;
    this->type=type;
    this->game=game;
    target_point=0;
    power=20;
    //隔一段时间发子弹
    //timer = new QTimer(this);
    //connect(timer,SIGNAL(timeout()),this,SLOT(attack()));
    //timer->start(firerate*1000);

    switch(type)
    {
     case 0 :
        {
            sprite.load(":/img/enemy");
            maxHp=300;
            currentHp=maxHp;
            movespeed=2;
            range=100;
            firerate=100;
            direction=1;
            break;
        }
    case 1:
        {
            gif=new QMovie(":/img/enemy1");
            gif->setSpeed(30);
            gif->start();

            maxHp=300;
            currentHp=maxHp;
            movespeed=2;
            range=100;
            firerate=100;
            direction=1;
            break;
        }

    case 2:{
        gif=new QMovie(":/img/enemy2");
        gif->setSpeed(30);
        gif->start();

        maxHp=300;
        currentHp=maxHp;
        movespeed=2;
        range=100;
        firerate=100;
        direction=1;
        break;
    }
    case 3:
        {
            gif=new QMovie(":/img/enemy3");
            gif->setSpeed(30);
            gif->start();

            maxHp=300;
            currentHp=maxHp;
            movespeed=2;
            range=100;
            firerate=100;
            direction=1;
            break;
        }
    }
}
Enemy::~Enemy()
{
   // delete gif;
}

void Enemy::setHp(int hurt)
{
    this->currentHp-=hurt;
    if(this->currentHp<=0)
    {
        this->game->remove_enemy(this);
        int money;
        switch(type)
        {
            case 0: money=100;
        case 1:money=200;
        case 2:money=200;
        case 3:money=200;
        }
        game->earn_money(money);
    }
}


void Enemy::move()
{

    int point_index=target_point;
    Scene scene=game->gameinfo->scene[game->id];
    QPoint next_pos=scene.road_point[point_index];

    int row_dir;
    int col_dir;
    int next_x;
    int next_y;

    if(next_pos.x()>pos.x())
        row_dir=1;
    else if(next_pos.x()<pos.x())
        row_dir=-1;
    else
        row_dir=0;
    if(next_pos.y()>pos.y())
        col_dir=1;
    else if(next_pos.y()<pos.y())
        col_dir=-1;
    else
        col_dir=0;

    if(row_dir==1) direction=1;
    else if(row_dir==-1) direction=2;
    else if(col_dir==1) direction=3;
    else direction=4;

    next_x=pos.x()+movespeed*row_dir;
    next_y=pos.y()+movespeed*col_dir;
    if(next_x>next_pos.x()&&row_dir== 1) next_x=next_pos.x();
    if(next_x<next_pos.x()&&row_dir==-1) next_x=next_pos.x();
    if(next_y>next_pos.y()&&col_dir== 1) next_y=next_pos.y();
    if(next_y<next_pos.y()&&col_dir==-1) next_y=next_pos.y();
    pos=QPoint(next_x,next_y);
    if(pos==next_pos) target_point++;
}

bool Enemy::check_turret(Game *game)
{
      current_turret=NULL;
      int flag=0;
      turret_set.clear();
      for(int i=0;i<game->turret_list.size();i++)
      {
          Turret *turret=game->turret_list.at(i);
          int tx=turret->pos.x();
          int ty=turret->pos.y();
          int nx=pos.x();
          int ny=pos.y();
          if(((tx-nx)*(tx-nx)+(ty-ny)*(ty-ny))<range*range)
          {
              flag=1;
              turret_set.insert(turret);
          }
      }
      if(flag==0)
      {
          return false;
      }
      current_turret=*(turret_set.begin());
      return true;
}

void Enemy::draw(QPainter *painter) const
{
    painter->save();
    //sprite.load(game->gif[type]->currentPixmap());


    //绘制血条
    QPoint healthPos = pos+QPoint(0,200);   //血条位置
    painter->setPen(Qt::NoPen);
    QBrush brush;
    //brush.setColor(getColor());
    brush.setColor(Qt::green);
    painter->setBrush(brush);
    QRect healthRect(healthPos,QSize((double)(currentHp/ maxHp)*health_bar_width,20));  //血条长宽
    painter->drawRect(healthRect);

    //绘制敌人
    painter->resetTransform();
    painter->translate(this->pos.x(),this->pos.y());
    QPixmap gifc(gif->currentPixmap());
    gifc=gifc.scaled(gifc.width()/2,gifc.height()/2);
    QPoint drawPos(-gifc.width()/2,-gifc.height()/2);
    //qDebug()<<"direct="<<direction;
    /*switch(direction)
    {
        case 1:{ painter->rotate(90); break;}
        case 2:{ painter->rotate(270); break;}
        case 3:{ painter->rotate(0); break;}
        case 4:{ painter->rotate(180); break;}
    }*/
    painter->drawPixmap(drawPos,gifc);
    painter->resetTransform();
    painter->restore();
}


QColor Enemy::getColor() const
{
    int r=0,g=255,b=0;
    if(currentHp>(maxHp/2))
    {
        r=255-(currentHp-maxHp/2)/(maxHp/2)*255;
    }
    else if(currentHp<(maxHp/2) && currentHp>0)
    {
        r=255;
        g=currentHp/(maxHp/2)*255;
    }
    else
    {
        r=255;
        g=0;
    }
    return QColor(r,g,b);
}

void Enemy::attack()
{
    if(atktime==firerate){
        atktime=0;
    if(this->game->check_turret(current_turret)){
        this->game->add_bullet(power,type,1,game,pos,0,current_turret);
    }
    }
    else atktime++;
}

QPoint Enemy::getPos()
{
    return pos;
}

void Enemy::UpdateMap()
{
    move();
}
